package com.kaifamiao.error3;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private final RedisTemplate<String, Serializable> limitRedisTemplate;
    private final int COUNT = 3;

    public LoginController(RedisTemplate<String, Serializable> limitRedisTemplate) {
        this.limitRedisTemplate = limitRedisTemplate;
    }

    @PostMapping ("/login")
    @LimitCount(key = "test",name = "测试接口",  prefix = "limit", period = 60, count = COUNT)
    public Map<String, Object> test(
            @RequestParam(required = true) String username,
            @RequestParam(required = true) String password, HttpServletRequest request) throws Exception {
        System.out.println(username);
        System.out.println(password);
        int submitCount = readAnnotationValues(request);

        submitCount=COUNT-submitCount;
        Map<String, Object> mp = new HashMap<>();
        if (StringUtils.equals("admin", username) && StringUtils.equals("123456", password)) {
            mp.put("code", 200);
            mp.put("msg", "登录成功");
            return mp;
        }
        mp.put("code", 500);
        mp.put("msg", "登录失败，还剩下"+submitCount +"次机会！");

        return mp;
    }
    @GetMapping("/")
    public ModelAndView show() throws NoSuchMethodException {
        return new ModelAndView("index");
    }

    public int readAnnotationValues( HttpServletRequest request) throws NoSuchMethodException{
        int submitCount =0;
        Method method = this.getClass().getMethod("test", String.class, String.class, HttpServletRequest.class);

        LimitCount limitCount = method.getAnnotation(LimitCount.class);

        if (limitCount != null) {
            String name = limitCount.name();
            String key = limitCount.key();
            String prefix = limitCount.prefix();
            int period = limitCount.period();
            int count = limitCount.count();
            // 使用这些值进行操作
            // 例如，打印出来
            System.out.print("Name: " + name);
            System.out.print("Key: " + key);
            System.out.print("Prefix: " + prefix);
            System.out.print("Period: " + period);
            System.out.println("Count: " + count);
            String redis_key= prefix+"_"+key+IPUtil.getIpAddr(request);
            log.error(redis_key);
            System.out.println(limitRedisTemplate.opsForValue().get(redis_key));
            submitCount = (int) limitRedisTemplate.opsForValue().get(redis_key);
            log.info(String.valueOf(submitCount));
//            return submitCount;
        }
        return submitCount;
    }
}